package cn.tedu._05egweibo01.controller;

import cn.tedu._05egweibo01.common.response.JsonResult;
import cn.tedu._05egweibo01.common.response.StateCode;
import cn.tedu._05egweibo01.mapper.CommentMapper;
import cn.tedu._05egweibo01.pojo.dto.CommentDTO;
import cn.tedu._05egweibo01.pojo.entity.Comment;
import cn.tedu._05egweibo01.pojo.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Api(tags = "评论模块")
@Slf4j
@RestController
@RequestMapping("v1/comment")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;


    @ApiOperation("用户评论")
    @PostMapping("insert")
    public JsonResult insertComment(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        UserVo user = (UserVo) session.getAttribute("user");
//        if (user == null){ //用户未登录
//            return new JsonResult(StateCode.NOT_LOGIN);
//        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
//        配置用户Id
        comment.setUserId(user.getId());
        commentMapper.InsetComment(comment);
        return  JsonResult.ok();
    }
    @ApiOperation("获取评论列表")
    @GetMapping("selectByWeiboId")
    @ApiImplicitParam(name = "wid",value = "微博Id",required = true,dataType = "long")
    public JsonResult selectByWeiboId(@RequestParam Long wid){

        return JsonResult.ok(commentMapper.selectByWeiboId(wid));
    }
}
